//
//  ZDMutiMenuView.m
//  1012GiftTips
//
//  Created by Apple on 15/10/21.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "ZDMutiMenuView.h"
#import "Masonry.h"
//#import "UIView+ZUtility.h"
#import "ZDLeftTableViewCell.h"
#import "ZDCollectionReusableView.h"
#import "ZDRightCollectionViewCell.h"


#import "Global1.h"

#define ZDLeftTableViewWidth 80
#define ZDCollectionCellHeight 100
#define ZDCollectionViewSectionHeaderHight 50
#define ZDCellSelectedColor ZDColor(240, 241, 244)

@interface ZDMutiMenuView () <UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UITableView *leftTableView;

@property (nonatomic, strong) UICollectionView *rightCollectionView;

@property (nonatomic, assign) NSInteger selectedIndex;


@end


@implementation ZDMutiMenuView

+ (instancetype)mutiMenuView
{
    return [[self alloc] init];
}

#pragma mark - xib加载
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder])
    {
        [self setUpSubViews];
    }
    
    return self;
}


#pragma mark - 代码加载
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.backgroundColor = [UIColor cyanColor];
        
        [self setUpSubViews];
    }
    
    return self;
}


#pragma mark - 添加子控件
- (void)setUpSubViews
{
    [self addSubview:self.leftTableView];
    [self addSubview:self.rightCollectionView];
    
    [self.leftTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.top.bottom.mas_equalTo(self);
        make.width.mas_equalTo(ZDLeftTableViewWidth);
    }];
    
    [self.rightCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.bottom.right.mas_equalTo(self);
        make.left.mas_equalTo(self.leftTableView.mas_right);
    }];
}



#pragma mark - leftTableView数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 17;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZDLeftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZDLeftTableViewCell class])];
    
    //    ZDLeftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZDLeftTableViewCell class]) forIndexPath:indexPath];
    
    cell.label.text = @"全部品类";
    
    cell.backgroundColor = ZDCellSelectedColor;
    
#warning 由于重用机制的影响，设置hidden后一定要在不满足条件的时候将其打开
    if (indexPath.row == self.selectedIndex)
    {
        cell.hiddenRedLine = NO;
        cell.label.textColor = [UIColor redColor];
        cell.backgroundColor = [UIColor whiteColor];
    }
    else
    {
        cell.hiddenRedLine = YES;
        cell.label.textColor = [UIColor blackColor];
        cell.backgroundColor = ZDCellSelectedColor;
    }
    
    return cell;
}

#pragma mark - leftTableView代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZDLeftTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    if (indexPath.row != self.selectedIndex)
    {
        self.selectedIndex = indexPath.row;
        
        [self.leftTableView reloadData];
        
        NSIndexPath *path = [NSIndexPath indexPathForItem:0 inSection:indexPath.row];
        
        [self.rightCollectionView scrollToItemAtIndexPath:path atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
    }
}


#pragma mark - rightCollectionView数据源方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 17;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 7;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZDRightCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ZDRightCollectionViewCell class]) forIndexPath:indexPath];
    
    cell.iconView.image = [UIImage imageNamed:@"Share_WeChatTimelineIcon"];
    cell.descLabel.text = @"猫狗大战";
    
    return cell;
}

/**
 *  设置组头view
 返回值:A configured supplementary view object. You must not return nil from this method.不能为nil
 This method must always return a valid view object. If you do not want a supplementary view in a particular case, your layout object should not create the attributes for that view. Alternatively, you can hide views by setting the hidden property of the corresponding attributes to YES or set the alpha property of the attributes to 0. To hide header and footer views in a flow layout, you can also set the width and height of those views to 0.
 */
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    ZDCollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([ZDCollectionReusableView class]) forIndexPath:indexPath];
    
    headView.titleLabel.text = @"双11";
    
#warning 由于重用机制的影响，设置hidden后一定要在不满足条件的时候将其打开
/****  虽然是隐藏头部view，看不到，但是size还在，影响美观  所以选择直接设置size为0，同样实现隐藏效果  ****/
//    if (indexPath.section == 0)
//    {
//        headView.hidden = YES;
//    }
//    else
//    {
//        headView.hidden = NO;
//    }
    
    return headView;
}

#pragma mark - rightCollectionView代理方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
}


#pragma mark - UICollectionViewDelegateFlowLayout代理方法
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    //设置第一组的head隐藏
    if (section == 0)
    {
        return CGSizeZero;
    }
    
    return CGSizeMake(self.width-ZDLeftTableViewWidth, ZDCollectionViewSectionHeaderHight);
}


#pragma mark - UIScrollViewDelegate方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == self.rightCollectionView)
    {
        ZDLog(@"%lf", scrollView.contentOffset.y);
        
//        NSArray *indexPaths = [self.rightCollectionView indexPathsForVisibleSupplementaryElementsOfKind:UICollectionElementKindSectionHeader];
        
        NSArray *indexPaths = [self.rightCollectionView indexPathsForVisibleItems];
        
        NSIndexPath *indexPath = indexPaths.firstObject;
        
        ZDLog(@"%@", NSStringFromCGPoint([self.rightCollectionView cellForItemAtIndexPath:indexPath].center));
        
        ZDLog(@"%ld---%ld", indexPath.section, indexPath.row);
        
        ZDLog(@"%@", self.rightCollectionView.visibleCells);
        
//        NSIndexPath *indexPath = [self.rightCollectionView indexPathForItemAtPoint:CGPointMake(50, 10)];
//        
//        ZDLog(@"%ld-----%ld", indexPath.section, indexPath.row);
    }
}


#pragma mark 懒加载
- (UITableView *)leftTableView
{
    if (!_leftTableView)
    {
        _leftTableView = [[UITableView alloc] init];
        
        _leftTableView.backgroundColor = ZDCellSelectedColor;
        _leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _leftTableView.showsVerticalScrollIndicator = NO;
        
        _leftTableView.dataSource = self;
        _leftTableView.delegate = self;
        
        //注册cell
        [_leftTableView registerNib:[UINib nibWithNibName:NSStringFromClass([ZDLeftTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([ZDLeftTableViewCell class])];
    }
    
    return _leftTableView;
}

- (UICollectionView *)rightCollectionView
{
    if (!_rightCollectionView)
    {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        
        layout.minimumInteritemSpacing = 10;
        layout.itemSize = CGSizeMake((ZDScreenSize.width - ZDLeftTableViewWidth - 30) / 3, ZDCollectionCellHeight);
        layout.sectionInset = UIEdgeInsetsMake(0, 5, 0, 5);   //作用于组
        
        
        _rightCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        
        _rightCollectionView.backgroundColor = [UIColor whiteColor];
//        _rightCollectionView.contentInset = UIEdgeInsetsMake(-24, 0, 0, 0);  //作用于collectionView  在滚动时，默认会是每一组都与上边产生一个间距
        
        _rightCollectionView.dataSource = self;
        _rightCollectionView.delegate = self;
        
        
        //注册section头
        [_rightCollectionView registerNib:[UINib nibWithNibName:NSStringFromClass([ZDCollectionReusableView class]) bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([ZDCollectionReusableView class])];
        //注册cell
        [_rightCollectionView registerNib:[UINib nibWithNibName:NSStringFromClass([ZDRightCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([ZDRightCollectionViewCell class])];
    }
    
    return _rightCollectionView;
}



@end
